﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace EHTWebsite.Code
{
    /// <summary>
    /// Author : toanvt
    /// CreateDate : 04/02/2012
    /// Description : Provider cho File
    /// </summary>
    public class FileProvider
    {
        /// <summary>
        /// Author : toanvt
        /// CreateDate : 04/02/2012
        /// Description : Lấy danh sách dùng cho phần quản trị -> trả về danh sách Files
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public static DataSet GetFilesForBackEnd(Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetFilesForBackEnd";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));

            DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);
            return ds;
        }

        /// <summary>
        /// Author : toanvt
        /// CreateDate : 04/02/2012
        /// Description : Lấy danh sách dùng cho phần front end -> trả về danh sách Files
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public static DataSet GetFilesForFrontEnd(Boolean isVN)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetFilesForFrontEnd";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@IsVN", isVN));

            DataSet ds = DataProvider.ExecuteDataSet(command);
            return ds;
        }

        public static Int32 IUFile(File theFile, bool IsAdd)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_IUFiles";
            command.CommandType = CommandType.StoredProcedure;
            SqlParameter theFileIDParameter = new SqlParameter();

            theFileIDParameter.ParameterName = "@FileIDOut";
            theFileIDParameter.Direction = ParameterDirection.Output;
            theFileIDParameter.DbType = DbType.Int32;

            command.Parameters.Add(theFileIDParameter);
            command.Parameters.Add(new SqlParameter("@FileID", theFile.FileID));
            command.Parameters.Add(new SqlParameter("@FileUrl", theFile.FileUrl));
            command.Parameters.Add(new SqlParameter("@MoTaVN", theFile.MoTaVN));
            command.Parameters.Add(new SqlParameter("@MoTaOther", theFile.MoTaOther));
            command.Parameters.Add(new SqlParameter("@Publish", theFile.Publish));
            command.Parameters.Add(new SqlParameter("@NumberOfOrder", theFile.NumberOfOrder));
            command.Parameters.Add(new SqlParameter("@IsAdd", IsAdd));

            DataProvider.ExecuteNoneQuery(command);

            return Int32.Parse(command.Parameters["@FileIDOut"].Value.ToString());
        }

        public static void DeleteFileByFileID(Int32 FileID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteFileByFileID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@FileID", FileID));

            DataProvider.ExecuteNoneQuery(command);
        }

        public static File GetFileByFileID(Int32 FileID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetFileByFileID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@FileID", FileID));

            DataSet ds = DataProvider.ExecuteDataSet(command);
            File theFile = null;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theFile = new File();
                theFile.FileUrl = ds.Tables[0].Rows[0]["FileUrl"].ToString();
                theFile.MoTaVN = ds.Tables[0].Rows[0]["MoTaVN"].ToString();
                theFile.MoTaOther = ds.Tables[0].Rows[0]["MoTaOther"].ToString();
                theFile.FileID = Int32.Parse(ds.Tables[0].Rows[0]["FileID"].ToString());
                theFile.Publish = Boolean.Parse(ds.Tables[0].Rows[0]["Publish"].ToString());
                theFile.NumberOfOrder = Int32.Parse(ds.Tables[0].Rows[0]["NumberOfOrder"].ToString());
            }

            return theFile;
        }

        public static void ChangePublishByFileID(Int32 fileID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_ChangePublishByFileID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@FileID", fileID));
            DataProvider.ExecuteNoneQuery(command);
        }

        public static void UpdateNumberOfOrderByFileID(Int32 fileID, Int32 numberOfOrder)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_UpdateNumberOfOrderByFileID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@FileID", fileID));
            command.Parameters.Add(new SqlParameter("@NumberOfOrder", numberOfOrder));
            DataProvider.ExecuteNoneQuery(command);
        }
    }
}